package com.ucar.util;

import com.ucar.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * 对话框帮助类
 * 
 * 
 */
public class DialogUtil {
	private static int SCREEN_WIDTH = 0;
	/**
	 * 创建转圈进度条对话框
	 * 
	 * @param context
	 * @return
	 */
	public static Dialog createLoadingViewDialog(Context context) {
		Dialog dialog = new Dialog(context, R.style.Dialog_Loading);
		dialog.setContentView(R.layout.dialog_loading);

		dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK
						&& event.getAction() == KeyEvent.ACTION_DOWN) {
				}
				return false;
			}

		});
		if ( SCREEN_WIDTH == 0 )
		{
			DisplayMetrics dm = Utils.getDisplayMetrics( context );
			SCREEN_WIDTH = dm.widthPixels > dm.heightPixels ? dm.heightPixels : dm.widthPixels;
		}
		
		WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
		p.height = WindowManager.LayoutParams.WRAP_CONTENT;
		p.width = (int) (SCREEN_WIDTH * 0.6);
		p.alpha = 0.9f;// 设置透明度
		dialog.getWindow().setAttributes(p);
		return dialog;
	}

	/**
	 * 创建选择对话框
	 * 
	 * @param context
	 * @param strTitleRes
	 *            标题
	 * @param strMsgRes
	 *            内容（可为空null）
	 * @param clickNoListener
	 * @param clickYesListener
	 * @return
	 */
	public static Dialog createTextQADialog(Context context,
			String strMsgRes, final OnClickNoListener clickNoListener,
			final OnClickYesListener clickYesListener) {
		final Dialog dialog = new Dialog(context, R.style.Dialog_QAD);
		dialog.setContentView(R.layout.dialog_text_qa);
		TextView tvMsg = (TextView) dialog.findViewById(R.id.dialog_qa_tv_msg);
		
		if (strMsgRes == null) {
			tvMsg.setVisibility(View.GONE);
		} else {
			tvMsg.setVisibility(View.VISIBLE);
			tvMsg.setText(strMsgRes);
		}

		dialog.findViewById(R.id.dialog_qa_btn_cancel).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						v.postDelayed(new Runnable() {
							public void run() {
								if (clickNoListener != null) {
									clickNoListener.onClickNo();
								}
							}
						}, 100);
					}
				});
		dialog.findViewById(R.id.dialog_qa_btn_ok).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						if (clickYesListener != null) {
							clickYesListener.onClickYes();
						}

					}
				});
		if ( SCREEN_WIDTH == 0 )
		{
			DisplayMetrics dm = Utils.getDisplayMetrics( context );
			SCREEN_WIDTH = dm.widthPixels > dm.heightPixels ? dm.heightPixels : dm.widthPixels;
		}
		WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
		p.width = (int) (SCREEN_WIDTH * 0.8);
		p.alpha = 0.9f;// 设置透明度
		dialog.getWindow().setAttributes(p);
		dialog.show();
		return dialog;
	}
	
	public interface OnClickNoListener {
		abstract void onClickNo();

	}

	public interface OnClickYesListener {
		abstract void onClickYes();

	}

	public interface OnClickYesDialogListener {
		abstract void onClickYes(Dialog dialog);

	}

	/**
	 * 创建OK对话框
	 * 
	 * @param context
	 * @param strRes
	 *            msg文字资源
	 * @param okL
	 *            OK按钮点击事件
	 * @return
	 */
	public static Dialog createTextOKDialog(Context context, int strRes,
			final OnClickYesListener clickYesListener) {
		final Dialog dialog = new Dialog(context, R.style.Dialog_QAD);
		dialog.setContentView(R.layout.dialog_text_ok);
		TextView tv = (TextView) dialog.findViewById(R.id.textok_tv);
		tv.setText(strRes);

		dialog.findViewById(R.id.textok_btn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						if (clickYesListener != null) {
							clickYesListener.onClickYes();
						}

					}
				});
		if ( SCREEN_WIDTH == 0 )
		{
			DisplayMetrics dm = Utils.getDisplayMetrics( context );
			SCREEN_WIDTH = dm.widthPixels > dm.heightPixels ? dm.heightPixels : dm.widthPixels;
		}
		WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
		p.height = WindowManager.LayoutParams.WRAP_CONTENT;
		p.width = (int) (SCREEN_WIDTH * 0.8);
		p.alpha = 0.9f;// 设置透明度
		dialog.getWindow().setAttributes(p);
		dialog.show();
		return dialog;
	}
	public static Dialog createTextOKDialog(Context context, String strRes,
			final OnClickYesListener clickYesListener) {
		final Dialog dialog = new Dialog(context, R.style.Dialog_QAD);
		dialog.setContentView(R.layout.dialog_text_ok);
		TextView tv = (TextView) dialog.findViewById(R.id.textok_tv);
		tv.setText(strRes);

		dialog.findViewById(R.id.textok_btn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						if (clickYesListener != null) {
							clickYesListener.onClickYes();
						}

					}
				});
		if ( SCREEN_WIDTH == 0 )
		{
			DisplayMetrics dm = Utils.getDisplayMetrics( context );
			SCREEN_WIDTH = dm.widthPixels > dm.heightPixels ? dm.heightPixels : dm.widthPixels;
		}
		WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
		p.height = WindowManager.LayoutParams.WRAP_CONTENT;
		p.width = (int) (SCREEN_WIDTH * 0.8);
		p.alpha = 0.9f;// 设置透明度
		dialog.getWindow().setAttributes(p);
		dialog.show();
		return dialog;
	}
	public static Dialog createViewQADialog(Context context, int strTitleRes,
			View view, final OnClickNoListener clickNoListener,
			final OnClickYesDialogListener clickYesDialogListener) {
		final Dialog dialog = new Dialog(context, R.style.Dialog_QAD);

		dialog.setContentView(R.layout.dialog_view_qa);
		ViewGroup vg = (ViewGroup) dialog
				.findViewById(R.id.dialog_view_qa_root);

		vg.addView(view, 1);
		TextView tvTitle = (TextView) dialog
				.findViewById(R.id.dialog_view_qa_tv_title);
		tvTitle.setText(strTitleRes);

		dialog.findViewById(R.id.dialog_view_qa_btn_cancel).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						v.postDelayed(new Runnable() {
							public void run() {
								if (clickNoListener != null) {
									clickNoListener.onClickNo();
								}
							}
						}, 100);
					}
				});
		dialog.findViewById(R.id.dialog_view_qa_btn_ok).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (clickYesDialogListener != null) {
							clickYesDialogListener.onClickYes(dialog);
						}

					}
				});
		if ( SCREEN_WIDTH == 0 )
		{
			DisplayMetrics dm = Utils.getDisplayMetrics( context );
			SCREEN_WIDTH = dm.widthPixels > dm.heightPixels ? dm.heightPixels : dm.widthPixels;
		}
		WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
		p.height = WindowManager.LayoutParams.WRAP_CONTENT;
		p.width = (int) (SCREEN_WIDTH * 0.8);
		p.alpha = 0.9f;// 设置透明度
		dialog.getWindow().setAttributes(p);
		dialog.show();
		return dialog;
	}

}
